<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请求方式" prop="methods" label-width="120px">
        <el-select v-model="queryParams.methods" placeholder="请选择请求方式" clearable size="small">
          <el-option label="post" value="post" />
          <el-option label="get" value="get" />
          <el-option label="delete" value="delete" />
          <el-option label="put" value="put" />
        </el-select>
      </el-form-item>
      <el-form-item label="接口host" prop="host" label-width="120px">
        <el-input
          v-model="queryParams.host"
          placeholder="请输入接口host"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接口地址" prop="apiurl" label-width="130px">
        <el-input
          v-model="queryParams.apiurl"
          placeholder="请输入接口地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态码" prop="status" >
        <el-input
          v-model="queryParams.status"
          placeholder="请输入状态码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:result:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:result:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="测试结果id" align="center" prop="resultId" />
      <el-table-column label="接口名称" align="center" prop="interfaceName" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="请求类型" align="center" prop="type" >
        <template slot-scope="scope">
          <span v-if="scope.row.type==='http'" >HTTP</span>
          <span v-if="scope.row.type==='https'">HTTPS</span>
        </template>
      </el-table-column>
      <el-table-column label="请求方法" align="center" prop="methods" >
        <template slot-scope="scope">
          <span v-if="scope.row.methods==='get'" style="color: #1e82e5;font-weight: bold;font-size:18px">get</span>
          <span v-if="scope.row.methods==='post'" style="color: #5100b9;font-weight: bold;font-size:18px">post</span>
          <span v-if="scope.row.methods==='delete'" style="color: #de1130;font-weight: bold;font-size:18px">delete</span>
          <span v-if="scope.row.methods==='put'" style="color: #aab000;font-weight: bold;font-size:18px">put</span>
        </template>
      </el-table-column>
      <el-table-column label="接口host" align="center" prop="host" width="180"/>
      <el-table-column label="接口地址" align="center" prop="apiurl" />
      <el-table-column label="接口超时时间(ms)" align="center" prop="timeout" />
      <el-table-column label="返回状态码" align="center" prop="status" >
        <template slot-scope="scope">
          <span v-if="scope.row.status==='200'" style="color: #14c01c;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='201'" style="color: #14c01c;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='202'" style="color: #14c01c;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='203'" style="color: #14c01c;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='204'" style="color: #14c01c;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='205'" style="color: #14c01c;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='206'" style="color: #14c01c;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='400'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='405'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='404'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='403'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='401'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='402'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='406'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='407'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='408'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='409'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='410'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='411'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='412'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='413'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='414'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='415'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='416'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else-if="scope.row.status==='417'" style="color: #003ec2;font-weight: bold;font-size:18px">{{scope.row.status}}</span>
          <span v-else style="color: red;font-weight: bold;font-size:18px">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="错误信息" align="center" prop="error" >
        <template slot-scope="scope">
          <span v-if="scope.row.error==='null'"></span>
          <span v-else style="color: #ff0000;font-weight: bold">{{scope.row.error}}</span>
        </template>
      </el-table-column>
      <el-table-column label="接口调用时间" align="center" prop="resultTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resultTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:result:edit']"
          >详细信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:result:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 显示interfaceResult详情对话框 -->

    <el-dialog :title="title" :visible.sync="open" class="spec-dialog" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <br>
          <el-form-item label="返回内容的数据类型：" label-width="60">{{ form.contentType }}</el-form-item>
          <el-form-item label="接口连接方式：" label-width="60">{{ form.connection }}</el-form-item>
        <el-col :span="12">
          <el-form-item label="TCP最长连接时间：" label-width="60">{{ form.keepAlive }}</el-form-item>
          <el-form-item label="接口发送数据的编码格式：" label-width="60">{{ form.transferEncoding }}</el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="接口请求头：" label-width="60">
            <br>
            <el-card class="box-card" style="margin-top:16px">

              <b-code-editor v-model="this.form.headers"
                             :auto-format="true" :smart-indent="true"
                             :indent-unit="4"
                             :readonly=true
                             :data="form.headers"
                             :line-wrap="false" ref="code">

              </b-code-editor>

            </el-card>
          </el-form-item>
          <el-form-item label="接口请求参数：" label-width="60">
            <br>
            <el-card class="box-card" style="margin-top:16px">

              <b-code-editor v-model="this.form.httpParams"
                             :auto-format="true" :smart-indent="true"
                             :indent-unit="4"
                             :readonly=true
                             :data="form.httpParams"
                             :line-wrap="false" ref="code">

              </b-code-editor>

            </el-card>
          </el-form-item>
          <el-form-item label="接口请求Body：" label-width="60">
            <br>
            <el-card class="box-card" style="margin-top:16px">

                <b-code-editor v-model="this.form.body"
                               :auto-format="true" :smart-indent="true"
                               :indent-unit="4"
                               :readonly=true
                               :data="form.body"
                               :line-wrap="false" ref="code">

                </b-code-editor>

            </el-card>
          </el-form-item>
          <el-form-item label="接口响应结果：" label-width="60">
            <br>
            <el-card class="box-card" style="margin-top:16px">
              <b-code-editor v-model="this.form.returnBody"
                             :auto-format="true" :smart-indent="true"
                             :indent-unit="4"
                             :readonly=true
                             :data="form.returnBody"
                             :line-wrap="false" ref="code">

              </b-code-editor>

            </el-card>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listResult, getResult, delResult, addResult, updateResult, exportResult } from "@/api/system/result";
import 'vue-json-pretty/lib/styles.css';
export default {
  name: "Result",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // interfaceResult表格数据
      resultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        interfaceName: null,
        userId: null,
        methods: null,
        host: null,
        apiurl: null,
        headers: null,
        timeout: null,
        httpParams: null,
        body: null,
        contentType: null,
        connection: null,
        keepAlive: null,
        transferEncoding: null,
        error: null,
        status: null,
        returnBody: null,
        resultTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询interfaceResult列表 */
    getList() {
      this.loading = true;
      listResult(this.queryParams).then(response => {
        this.resultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        resultId: null,
        interfaceName: null,
        userId: null,
        methods: null,
        host: null,
        apiurl: null,
        headers: null,
        timeout: null,
        httpParams: null,
        body: null,
        contentType: null,
        connection: null,
        keepAlive: null,
        transferEncoding: null,
        error: null,
        status: null,
        returnBody: null,
        resultTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.resultId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加interfaceResult";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const resultId = row.resultId || this.ids
      getResult(resultId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "接口返回结果";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.resultId != null) {
            updateResult(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addResult(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const resultIds = row.resultId || this.ids;
      this.$confirm('是否确认删除interfaceResult编号为"' + resultIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delResult(resultIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有interfaceResult数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportResult(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style lang="scss">
.spec-dialog .el-dialog__body {
  padding: 3px 30px;
  overflow-y: auto;
  height: calc(100vh - 140px);
}
</style>
